package tjs.study.notes.dotest.jdk.thread;

/**
 * Created by Nicky.Tang on 2021/11/16 4:58 下午
 *
 * @since 02.12.10
 */
public class DoTestOfWait {
    private String name;

    public static void main(String[] args) {
        testSend();
    }

    private static void testSend() {
        DoTestOfWait doTestOfWait = new DoTestOfWait();
        new Thread(() -> {
            System.out.println("t1 已启动");
            if (doTestOfWait.name == null) {
                synchronized (doTestOfWait) {// 持有锁，才能wait加入等待队列
                    try {
                        doTestOfWait.wait();
                        //doTestOfWait.wait(3000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("t1 name=" + doTestOfWait.name);
        }).start();
        new Thread(() -> {
            System.out.println("t2 已启动");
            try {
                Thread.currentThread().sleep(2000);
                doTestOfWait.name = "我是t2,付个值";
                synchronized (doTestOfWait) {// 持有锁，才能唤醒等待队列
                    doTestOfWait.notify();
                    //doTestOfWait.notifyAll();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    public synchronized void doa() throws Exception {
        System.out.println("a static sart");
        //Thread.currentThread().sleep(3000);
        //this.wait();
        System.out.println("a static end");
    }

}
